﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace LabMS.Web.BaseData
{
    public partial class AddStudentInfo :Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("XSSJ");
            strPageUserType.Add("mana");

            /////数据库里多了一个StudentPhotoType
            if (!IsPostBack)
            {
                InitBind();
            }
            tbC.Style.Add("display", "none");
            tbM.Style.Add("display", "none");
            tbClass.Attributes.Add("readOnly", "true");
            tbMajor.Attributes.Add("readOnly", "true");
            //FileUpload1.Style.Add("display","none");
        }

        protected void InitBind()
        {
            //性别
            ExtendBLL.Dictionary BD = new ExtendBLL.Dictionary();
            ddlSex.DataSource = BD.GetItemListByDClass("201");
            ddlSex.DataValueField = "Code";
            ddlSex.DataTextField = "Name";
            ddlSex.DataBind();
            //年级
            ExtendBLL.SysSetting ES = new ExtendBLL.SysSetting();
            int Year = ES.GetCurrentSetting().CurrentYear;
            int S = int.Parse(ES.GetCurrentSetting().CurrentSemester.Value.ToString());
            ddlLJ.DataSource = Common.GradeHelper.GetCurrentGrade(Year, S, false);
            ddlLJ.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            #region 验证
            if (string.IsNullOrEmpty(tbCode.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page, "", "学号不能为空！");
                tbCode.Focus();
            }
            else if (string.IsNullOrEmpty(tbName.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page, "", "姓名不能为空！");
                tbName.Focus();
            }
            #endregion
            else
            {
                LabMS.Model.Student MStudent = new LabMS.Model.Student();
                
                ExtendBLL.Student EStudent = new ExtendBLL.Student();
                if (EStudent.IsStudentCodeExits(tbCode.Text.Trim()) == -1)
                {
                    Common.JShelper.JSAlert(this.Page, "", "该学号已经存在！");
                    tbCode.Text = "";
                    tbCode.Focus();
                }
                else
                {
                    MStudent.Student_Code = tbCode.Text.Trim();

                    MStudent.Student_Name = tbName.Text.Trim();
                    MStudent.Student_Sex = ddlSex.SelectedValue;
                    MStudent.Student_Summary = tbBZ.Text.Trim();
                    if (!string.IsNullOrEmpty(tbC.Text.Trim()))
                    {
                        MStudent.ClassID = int.Parse(tbC.Text.Trim()); //存ID
                    }
                    if (!string.IsNullOrEmpty(tbM.Text.Trim()))
                    {
                        MStudent.Student_Pro = int.Parse(tbM.Text.Trim());//存ID
                    }
                    if (!string.IsNullOrEmpty(tbRxly.Text.Trim()))
                    {
                        string[] DateTimeList = { "yyyy/MM/dd", "yyyy/MM", "yyyy/M", "yyyy-MM-dd", "yyyy-M-d", "yyyyMM", "yyyyMMdd", "yyyy-MM" };
                        MStudent.Student_EntryDate = DateTime.ParseExact(tbRxly.Text.Trim(), DateTimeList, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AllowInnerWhite);
                        //MStudent.Student_EntryDate = DateTime.ParseExact(tbRxly.Text.Trim(), "yyyyMM", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AllowInnerWhite);
                    }
                    MStudent.Student_Grade = ddlLJ.SelectedValue;

                    //#region 上传文件
                    //if (FileUpload1.HasFile)//如果有文件
                    //{

                    //    string FileName = FileUpload1.PostedFile.FileName;//文件名
                    //    int Length = FileUpload1.PostedFile.ContentLength;//文件大小
                    //    if (Length > 1024 * 1024)
                    //    {
                    //        Common.JShelper.JSAlert(this.Page, "", "图片文件不能大于1M！");
                    //    }
                    //    else
                    //    {
                    //        string Ext = FileName.Substring(FileName.LastIndexOf(@"."));//取出后缀
                    //        if (Ext != ".jpg" && Ext != ".bmp" && Ext != ".gif")
                    //        {
                    //            Common.JShelper.JSAlert(this.Page, "", "图片格式不正确！");
                    //            return;
                    //        }
                    //        else
                    //        {
                    //            //判断存放文件的路径是否存在
                    //            string Path = Server.MapPath("UploadFile") + "\\";
                    //            System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(Path);
                    //            if (!dirInfo.Exists)
                    //            {
                    //                dirInfo.Create();
                    //            }
                    //            else
                    //            {
                    //                string fullfilename = Guid.NewGuid() + Ext;
                    //                try
                    //                {
                    //                    FileUpload1.PostedFile.SaveAs(fullfilename);//保存文件
                    //                    //保存到数据库中
                    //                    byte[] PhotoArray = new byte[Length];
                    //                    System.IO.Stream photoStream = FileUpload1.PostedFile.InputStream;
                    //                    photoStream.Read(PhotoArray, 0, Length);

                    //                    MStudent.Student_Photo = PhotoArray;//相片
                    //                }
                    //                catch
                    //                {
                    //                    Common.JShelper.JSAlert(this.Page, "", "图片上传失败");
                    //                }
                    //            }
                    //        }
                    //    }
                    //}
                    //#endregion
                  
                    LabMS.BLL.Student BStudent = new LabMS.BLL.Student();
                    int count = BStudent.Add(MStudent);
                    if (count > 0)
                    {
                        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script type='text/javascript'>alert('新增成功！');window.opener.location.href=window.opener.location.href;self.close();</script>");
                    }
                    else
                    {
                        Common.JShelper.JSAlert(this.Page, "", "新增失败");
                    }
                    
                }
            }
        }
    }
}
